@echo off

set m_app_filename=
set app_findReult=0
for %%i in (../hex/*.hex) do (
set /a app_findReult+=1
set m_app_filename=..\hex\%%i
)
echo.
if %app_findReult% neq 1 goto Copy_Condition_Error

echo %m_app_filename%
copy %m_app_filename% .\App.hex /Y
..\..\..\tools\nrfutil settings generate --family NRF52 --application App.hex --application-version 1 --bootloader-version 1 --bl-settings-version 1 bootloader_settings.hex
..\..\..\tools\nrfutil settings display bootloader_settings.hex
mergehex -m rebuild_dfu\bootloader.hex bootloader_settings.hex -o BL.hex
if %errorlevel% neq 0 goto Build_Error
goto Build_Okay

:Copy_Condition_Error
echo The source app HEX files (%app_findReult%)!=1 
echo Please check it first.
echo.
pause
goto Build_Okay

:Build_Error
echo An error occurred.
echo.
pause
goto Build_Okay

:Build_Okay
echo.
echo * BL + APP  (BL.hex + App.hex)
